$(document).ready(function() {
	// 填充文章列表数据
	$.ajax({
		type: "GET",
		url: "http://localhost:8081/article/list",
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			$.each(json, function(i, item) {
				// if(i === 0) {
				// 	// 默认填充第一篇文章的评论信息
				// 	addCommentList(item.articleId)
				// }
				$('#articleList').append('<option articleId="' + item.articleId + '">' + item.articleTitle + '</option>');
			});
		}
	});

	// 填充评论列表信息
    $.ajax({
        url: "http://localhost:8081/comment/list",
        type: "GET",
        dataType: "json",
        success: function(json) {
            $.each(json, function(i, item) {
                $('#tbody-comments').append(
                    '<tr><td>' + +item.commentId +
                    '</td><td>' + item.commentContent +
                    '</td><td>' + item.article.articleTitle +
                    '</td><td>' + item.user.userName +
                    '</td><td>' + item.commentLikeCount +
                    '</td><td>' + item.commentDate +
                    '</td><td><button class="btn btn-danger deleteBtn" onclick="deleteArticleComment(\'' + item.commentId + '\')"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
                //$('#articleList').append('<option articleId="' + item.articleId + '">' + item.articleTitle + '</option>');
            });
            $('#dataTables-comments').dataTable();
        }
    });

});


// 根据ID填充评论列表的信息
function addCommentList(id) {
	$.ajax({
		type: "get",
		url: "http://localhost:8081/admin/comment/sort/" + id,
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		success: function(json) {
			// 先要清空原来的数据
			$('#tbody-comments').html("");
			$.each(json, function(i, item) {
				$('#tbody-comments').append(
                    '<tr><td>' + +item.commentId +
                    '</td><td>' + item.commentContent +
                    '</td><td>' + item.article.articleTitle +
                    '</td><td>' + item.user.userName +
                    '</td><td>' + item.commentLikeCount +
                    '</td><td>' + item.commentDate +
					'</td><td><button class="btn btn-danger deleteBtn" onclick="deleteArticleComment(\'' + item.commentId + '\')"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
			});
			$('#dataTables-comments').dataTable();
		}
	});
}

// 监听文章Select改变，改变注入相应的评论
document.getElementById("articleList").onchange = function() {
	var articleId = $('#articleList option:selected').attr("articleId");
    if (articleId === "") {
        var url = "http://localhost:8081/comment/list";
    } else {
        var url = "http://localhost:8081/comment/list/sort/" + articleId;
    }
	// addCommentList(articleId);
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function(json) {
            // 先要清空原来的数据
            $('#tbody-comments').html("");
            $.each(json, function(i, item) {
                $('#tbody-comments').append(
                    '<tr><td>' + +item.commentId +
                    '</td><td>' + item.commentContent +
                    '</td><td>' + item.article.articleTitle +
                    '</td><td>' + item.user.userName +
                    '</td><td>' + item.commentLikeCount +
                    '</td><td>' + item.commentDate +
                    '</td><td><button class="btn btn-danger deleteBtn" onclick="deleteArticleComment(\'' + item.commentId + '\')"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
            });
            $('#dataTables-comments').dataTable();
        }
    });
};

// 删除按钮点击事件
function deleteArticleComment(id) {
	$('#confirmBtn').attr("articleCommentId", id);
	$('#myModal').modal();
};

// 确认删除按钮点击事件
$('#confirmBtn').click(function() {
	var id = $(this).attr("articleCommentId");
	$.ajax({
		type: "DELETE",
		url: "http://localhost:8081/admin/comment/" + id,
		success: function(result) {
			// 刷新页面
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
		},
		error: function() {
			location.reload();
		}
	});
});